package com.longcai.cm;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.ITypeConvert;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.config.rules.IColumnType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.fill.Column;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author libingxing
 */
public class NewMybatisGenerator {
    //    业务
    // private static final String PROJECT_PATH_SERVICE = "/Users/libingxing/Desktop/git/car/service";
    private static final String PROJECT_PATH_SERVICE = "C:\\javaproject\\car\\java\\service";
    //    实体
    // private static final String PROJECT_PATH_DOMAIN = "/Users/libingxing/Desktop/git/car/domain";
    private static final String PROJECT_PATH_DOMAIN = "C:\\javaproject\\car\\java\\domain";
    //      业务包名
    private static final String PACKAGE_NAME = "/com/longcai/cm/";
//      系统包名
//    private static final String PACKAGE_NAME = "/com/longcai/cm/moudules/";

    public static void main(String[] args) {
//        模块名称
        final String moduleName = "";
//        数据库表
        List<String> tables = new ArrayList<>();

//        tables.add("fa_dschool");

        // tables.add("z_third_car");

//        tables.add("ja_banner");
//        tables.add("ja_vip_info");
//        tables.add("ja_member_asset");
//        tables.add("fa_member_collection");
//        tables.add("ja_member_log_integral");
//        tables.add("ja_member_vip");
//        tables.add("ja_navigator");
//        tables.add("ja_order");
//        tables.add("ja_service_info");
//        tables.add("ja_shop");
//        tables.add("fa_member");
//        tables.add("ja_coupon");
//        tables.add("ja_goods_coupon");
//        tables.add("ja_goods_info");
//        tables.add("v_good_info_list");
//        tables.add("ja_member_coupons");
//        tables.add("ja_member_opinion");
//        tables.add("ja_help_center");
//        tables.add("ja_order_refund_apply");
//        tables.add("ja_order_comment");
        tables.add("ja_car_manage");
//        tables.add("v_order_list");

//        tables.add("v_member_coupon_list");
//       tables.add("v_member_vip_list");
//        tables.add("v_collection_list");
//        tables.add("ja_version_control");

//        tables.add("ja_car_proxy_title");
//        tables.add("ja_car_proxy_info");

        Map<OutputFile, String> filePath = new HashMap<>(5);
        filePath.put(OutputFile.mapperXml, PROJECT_PATH_DOMAIN + "/src/main/resources/mybatis/" + moduleName + "/");
        filePath.put(OutputFile.entity, PROJECT_PATH_DOMAIN + "/src/main/java" + PACKAGE_NAME + moduleName + "/domain/");
        filePath.put(OutputFile.controller, PROJECT_PATH_SERVICE + "/src/main/java" + PACKAGE_NAME + moduleName + "/controller/");
        filePath.put(OutputFile.service, PROJECT_PATH_SERVICE + "/src/main/java" + PACKAGE_NAME + moduleName + "/service/");
        filePath.put(OutputFile.serviceImpl, PROJECT_PATH_SERVICE + "/src/main/java" + PACKAGE_NAME + moduleName + "/service/impl/");
        FastAutoGenerator.create(new DataSourceConfig.Builder("jdbc:mysql://124.70.5.39:3306/cars_wzpu_cn", "cars_wzpu_cn", "Le5xS2RmhaNwxwNP")
                        .typeConvert(new MySqlTypeConvertCustom()))
                .globalConfig(builder -> builder
                        //作者
                        .author("LBX")
//                            不打开文件夹
                        .disableOpenDir()
                        //输出路径(写到java目录)
                        .outputDir(PROJECT_PATH_DOMAIN + "/src/main/java")
                        //开启swagger
                        .enableSwagger()
//                        时间类型 要date
                        .dateType(DateType.ONLY_DATE).commentDate("yyyy-MM-dd")
                        //开启覆盖之前生成的文件
                        .fileOverride()
                ).packageConfig(builder -> builder.parent("com.longcai.cm")
//                            模块名称
                        .moduleName(moduleName).entity("domain").service("service").serviceImpl("serviceImpl").controller("controller").mapper("dao").xml("mapper").pathInfo(filePath)
                ).strategyConfig(builder -> builder.addInclude(tables)
//                            表前缀过滤
                        .addTablePrefix("").serviceBuilder().formatServiceFileName("%sService").formatServiceImplFileName("%sServiceImpl").entityBuilder()
//                            lombok
                        .enableLombok()
//                            实体类链式编程
                        .enableChainModel()
                        .addTableFills(new Column("create_time", FieldFill.INSERT))
//                            Id类型
                        .idType(IdType.ASSIGN_UUID)
//                            逻辑删除字段
//                      .logicDeleteColumnName("deleted")
//                            说明注释
                        .enableTableFieldAnnotation()
                        .controllerBuilder().formatFileName("%sController")
//                            @RestController
                        .enableRestStyle()
                        .mapperBuilder().superClass(BaseMapper.class)
                        .formatMapperFileName("%sMapper")
//                        允许@mapper
                        .enableMapperAnnotation()
                        .formatXmlFileName("%sMapper"))
//        使用Freemarker引擎模板，默认的是Velocity引擎模板
                .templateEngine(new FreemarkerTemplateEngine()).templateConfig(builder -> builder
                        //  为空是只生成实体类，不为空是生成所有
                        .service("/templates/base-ftl/service")
                        .serviceImpl("/templates/base-ftl/serviceImpl")
                        .mapper("/templates/base-ftl/mapper")
                        .controller("/templates/base-ftl/controller")
//                        .mapper("")
//                        .service("")
//                        .serviceImpl("")
//                        .controller("")
                        .mapperXml("")).execute();
    }

    /**
     * 自定义类型转换
     */
    static class MySqlTypeConvertCustom extends MySqlTypeConvert implements ITypeConvert {
        @Override
        public IColumnType processTypeConvert(GlobalConfig globalConfig, String fieldType) {
            String t = fieldType.toLowerCase();
            if (t.contains("tinyint(1)")) {
                return DbColumnType.INTEGER;
            }
            if (t.contains("timestamp(0)")) {
                return DbColumnType.DATE;
            }
            return super.processTypeConvert(globalConfig, fieldType);
        }
    }
}
